<?php
 set_time_limit(0);
require_once 'common.inc.php';
require_once 'config.inc.php';
require_once MCC_ROOT.'/include/autochinese.php';

error_reporting(0);
$hashl=$_POST['hash'];
$nnid=$_POST['nn'];
$dsid    = new dbstuff();        // DS数据库连接标识符
$dsid->connect($dsdbhost,$dsdbuser,$dsdbpw,$dsdbname,1);

$lhq = $dsid->query("select * from localhash_1 where hash='$hashl'");
$lhrt = $dsid->fetch_array($lhq);
if($lhrt)
{
	$arears=$dsid->query("select ip,port from area where btcid='$lhrt[btcid]'");
	
	if($areart=$dsid->fetch_array($arears))
	{
		$btsenddata=pro_deletecontent($hashl);
		//print_r();
		//echo "ip=".long2ip($areart['ip']).'prot='.$areart['port'].'data='.$btsenddata;
		//die();
		connecthost(long2ip($areart['ip']),$areart['port'],$btsenddata);//发送协议
	
	}		
	
}
   //删除数据
 $p_infohash = trim($hashl);
if(!isset($p_infohash) || strlen($p_infohash)<40 || empty($p_infohash))
{
	exit('infohash is worng');
}
	
//删除三个东西：attach，hotfile,hothtml,还有同时也要删除attach中容量小于200的种子文件

//删除具体文件
if(file_exists('../push/attach/'.$p_infohash.'.torrent'))
{
 @unlink('../push/attach/'.$p_infohash.'.torrent');
 //echo "<br>正在删除../push/attach/".$p_infohash.".torrent";
}		

//读取attach文件夹所有文件，循环判断，容量小于200的则删除文件

$dirhander = opendir('../push/attach');
while ($file=readdir($dirhander))
{
	if($file!='.'&&$file!='..')
	{
		if(filesize("../push/attach/$file")<200)
		{
			@unlink("../push/attach/$file");
			//echo "<br>正在删除../push/attach/$file……";
		}
	}
}	

//读取hotfile文件夹所有文件，循环删除文件

$dirhander = opendir('../push/hotfile');
while ($file=readdir($dirhander))
{
	if($file!='.'&&$file!='..')
	{
		@unlink("../push/hotfile/$file");
	}
}	
//读取hothtml文件夹所有文件，循环删除文件

$dirhander = opendir('../push/hothtml');
while ($file=readdir($dirhander))
{
	if($file!='.'&&$file!='..')
	{
		@unlink("../push/hothtml/$file");
	}
}	

$dsid->query("delete from localhash_1 where hash='$hashl'");
if(!empty($p_infohash)&&!empty($nnid))
echo $nnid;

function prop_buildcontent($hash){
	$temp=chr(64).chr(1).chr(4);
	$temp.=$hash.".torrent\t\t\n";
	return $temp;
	
}

function pro_deletecontent($hash){
	$temp=chr(96).chr(1).chr(4);
	$temp.=$hash."\t\t\n";
	return $temp;
}

function connecthost($host,$port,$btcontent,$timeout=20){
	$fp=fsockopen($host,$port,$errno,$errstr,$timeout);
	//!$fp ?exit('不能连接服务器！！') : exit('suscc!');
	fwrite($fp,$btcontent);
	$serverdata=fgets($fp,1024);
	fclose($fp);
	return $serverdata.'|'.date("Y-m-d H:i:s")."\r\n";
}
?>
